<?php

/**
 * 文章内页
 */
class ArticleAction extends HomeBaseAction {

    /**
     * 文章详情页
     * @return [type] [description]
     */
    public function index() {

        if(!$_GET['id']){
            echo '404 Not Found!';exit;
        }
        
        $id = I('get.id');

        $article = M('posts')->where('id='.$id)->find();

        // print_r($article);
        // 如果文章不存在
        if(empty($article)){
            echo '404 Not Found !';exit;
        }
        //存在 阅读view+1
        M('posts')->where('id='.$id)->setInc('view');
        // 获取栏目ID
        $term_id = $article['term_id'];

        if(I('get.location')){
            $this->location = I('get.location');
        }else{
            $this->location = $term_id;
        }
        
        $term_obj = new TermsModel();
        // 获取栏目信息
        $term = $term_obj->where("term_id=".$term_id)->find();

        $parents = get_parents($term_id);
        // pe($parents);
        $this->assign('parents',$parents);


        // 
        $article['smeta'] = json_decode($article['smeta'],1);
        $article['product_info'] = json_decode($article['product_info'],1);
        $article['xijie'] = json_decode($article['xijie'],1);
        $article['shijing'] = json_decode($article['shijing'],1);

        $this->assign('article',$article);
        $this->assign("term",$term);
        
        // 获取上下篇
        $next = M('posts')->where("status <> 0 and id > $id and term_id = $term_id")->order('id')->limit(1)->find();
        $last = M('posts')->where("status <> 0 and id < $id and term_id = $term_id")->order('id desc')->limit(1)->find();
        $this->assign('next',$next);
        $this->assign('last',$last);

        $tplname = $term["one_tpl"]?$term["one_tpl"]:'article';
        // $tplname = sp_get_apphome_tpl($tplname, "article");
        $this->display(":$tplname");
        // 上一篇 下一篇 代码
        // select * from news where id<$id order by id desc limit 0,1 
        // select * from news where id>$id order by id desc limit 0,1 
    }  



}
?>
